package com.stlm2.controller.recharge;

import com.stlm2.dems.recharge.dao.RechargeDao;
import com.stlm2.dems.entity.OrderState;
import com.stlm2.dems.recharge.entity.Recharge;
import com.stlm2.dems.recharge.service.RechargeService;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Author: fuchunyu
 * Date: 2017/6/20
 * Description：余额充值类
 */
@Controller
@RequestMapping("/rechargeC")
public class RechargeController {

    @Resource
    private UserService userService;
    @Resource
    private RechargeService rechargeService;
    @Resource
    private RechargeDao rechargeDao;

    /**
     * Author: fuchunyu
     * Date: 2017/6/20
     * Description：添加充值订单
     */
    @ResponseBody
    @RequestMapping(value = "/add.json",produces = "application/json;charset=utf-8")
    public String add(String companyCode,String token, String money,String fluxId,String flux, ModelMap map,String phone){
        User user=userService.getByToken(token);
        if(user==null){
            return CodeContent.getModelByCode(-1,null,null);
        }

        if(!NumberUtils.isDigits(fluxId))
            return CodeContent.returnFailure("流量ID错误！");
        if(!NumberUtils.isDigits(flux))
            return CodeContent.returnFailure("请检查充值的流量!");

        Recharge recharge=rechargeService.addRecharge(user.getId(),money,Integer.parseInt(fluxId),Integer.parseInt(flux),phone);
        if(recharge==null){
            return CodeContent.getModelByCode(0,"订单生成失败！",null);
        }
        map.put("code",recharge.getCode());
        return CodeContent.getModelByCode(1,null,map);
    }

    /**
     * Author：fuchunyu
     * Date：2017/7/11
     * Description：查询未支付的订单
     */
    @ResponseBody
    @RequestMapping("/queryCreate.json")
    public String queryCreate(String companyCode,String token,ModelMap model){

        User user=userService.getByToken(token);
        if(user==null){
            return CodeContent.getModelByCode(-1,null,null);
        }
        //查询超过两小时未支付的订单，把状态改成取消状态
        List<Recharge> overRecharge=rechargeService.getOverRechageByUserId(user.getId());
        for (Recharge over:overRecharge) {
            over.setOrderState(OrderState.Cancel);
            over.setCancleTime(new Date());
            rechargeService.updateEntity(over);
        }

        //查询待支付的订单
        List<Recharge> recharge =rechargeService.getByUserId(user.getId());

        return CodeContent.getModelByCode(1,null,recharge);
    }
    /**
     * Author：fuchunyu
     * Date：2017/7/11
     * Description：删除订单
     */
    @ResponseBody
    @RequestMapping("/delOrder.json")
    public String delOrder (String companyCode,String token,ModelMap map,String id){
        if(StringUtils.isEmpty(id)){
            map.put("code", 0);
            map.put("msg", "删除ID为NULL");
            return new JSONObject(map).toString();
        }
        if(!NumberUtils.isDigits(id)){
            map.put("code", 0);
            map.put("msg", "非法的ID");
            return new JSONObject(map).toString();
        }
        Recharge recharge=rechargeDao.getById(NumberUtils.toInt(id));
        if(recharge==null){
            map.put("code", 0);
            map.put("msg", "获取记录为NULL");
            return new JSONObject(map).toString();
        }
        recharge.setCancleTime(new Date());
        recharge.setOrderState(OrderState.Cancel);
        rechargeService.updateEntity(recharge);

        map.put("code", 1);
        map.put("msg", "success");
        return new JSONObject(map).toString();

    }

    @ResponseBody
    @RequestMapping(value = "/getAllRechargeRecord", produces = "application/json;charset=UTF-8")
    public String getAllRechargeRecord(ModelMap map, String page, String token, String userName, String stateValue, String payChannelValue, String code, String transactionNo) {
        String date = "";
        try {
            date = rechargeService.getAllRechargeRecord(map, page, token, userName, stateValue, payChannelValue, code, transactionNo);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "充值记录获取失败", map);
        }

        return date;
    }


}
